/*
* BlowerDesService.java
* Created on  2015/8/15 11:07
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/8/15       xfz    初始版本
*
*/

package com.infitecs.eyas.blower.service;

import com.infitecs.eyas.blower.dao.BlowerDesDao;
import com.infitecs.eyas.blower.model.BlowerDes;
import com.infitecs.eyas.boiler.dao.AirGasParameterDao;
import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.nature.dao.NatureDao;
import com.infitecs.eyas.nature.model.Nature;
import com.infitecs.eyas.oncefan.model.OnceFanDes;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 类的描述信息
 *
 * @author xfz
 * @version 1.0.1
 */
@Service
public class BlowerDesService {

    @Autowired
    private BlowerDesDao blowerDesDao;

    @Autowired
    private SessionService sessionService;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private AirGasParameterDao airGasParameterDao;
    @Autowired
    private NatureDao natureDao;
    @Autowired
    private OperateFieldDao operateFieldDao;

    public void saveValueOfBlowerDes(BlowerDes blowerDes) {
        blowerDes.setCaseId(sessionService.getCaseId());
        blowerDesDao.saveValueOfBlowerDes(blowerDes);
        blowerDes.setParamName(sessionService.getE() + blowerDes.getParamName());
        sessionService.putParamMap(blowerDes);
    }

    public BlowerDes getBlowerDes() {
        int caseId = sessionService.getCaseId();
        return blowerDesDao.getBlowerDes(caseId);
    }

    public void addBlowerDes() {
        int caseId = sessionService.getCaseId();
        blowerDesDao.addBlowerDes(caseId);
    }

    public Double getSelected(int caseId) {
        return blowerDesDao.getSelected(caseId);
    }

    public void blowerDesDesSave(BlowerDes blowerDes) {
        if (blowerDes.getSfjglState().equals("0")) {
            Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(blowerDes.getCaseId());
            if (oldEfficiency == null) {
                throw new ValidateErrorException("锅炉数据未填写或未全部填写，不能引用锅炉数据！");
            }
        }
        blowerDesDesDataSave(blowerDes);
    }

    private void blowerDesDesDataSave(BlowerDes blowerDes) {
        if (blowerDes.getSfjglState().equals("0")) {
            AirGasParameter airGasParameter = airGasParameterDao.getAirGasParameter(blowerDes.getCaseId());
            Efficiency efficiency = efficiencyDao.getEfficiencyByPowerCaseId(blowerDes.getCaseId());
            //获取锅炉参数
            //E3
            blowerDes.setE3(airGasParameter.getHearthOverOutAirCoefficient());
            //E4
            blowerDes.setE1(efficiency.getE93());
            //E6
            blowerDes.setE5(efficiency.getE52());
            //E8
            blowerDes.setE2(airGasParameter.getE43());
            blowerDes.setE6(airGasParameter.getWarehouse() + 0.0);
            //20
            blowerDes.setE4(airGasParameter.getAirPreheaterAirLeakageCoefficient());
            blowerDes.setE7(airGasParameter.getOnceWindRate());
        }
        Nature nature = natureDao.getNatureByPowerCaseId(blowerDes.getCaseId());
        blowerDes.setE15(nature.getAnnualAverageTemperature());
        blowerDes.setE16(nature.getSummerOutdoorCalculateTemperature());
        BlowerDes oldData = blowerDesDao.getBlowerDes(blowerDes.getCaseId());
        if (oldData == null) {
            blowerDesDao.addBlowerDesData(blowerDes);
        } else {
            blowerDesDao.updateBlowerDesData(blowerDes);
        }
        OperateField operateField = new OperateField();
        operateField.setSfjglState(blowerDes.getSfjglState());
        operateField.setCaseId(blowerDes.getCaseId());
        operateFieldDao.updateOperateFieldBlowerDes(operateField);
    }
}
